﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.Global;
using MySql.Data.MySqlClient;
using StockTrader.BusinessService.DataContract.Global;
using System.Data;
using StockTrader.DAL.MySQLHelper;

namespace StockTrader.DAL.MySQL.Global
{
    public class StocksInfo : IStocksInfo
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters
        private const string PARM_STOCKID = "@STOCK_ID";
        private const string PARM_NAME = "@NAME";
        private const string PARM_INDUSTRY = "@INDUSTRY";
        private const string PARM_MARKET = "@MARKET";
        private const string PARM_SYMBOL = "@SYMBOL";
        private const string PARM_LANG = "@LANG";
        #endregion

        #region SQL
        private const string INSERT_STOCKSINFO = "INSERT INTO G_STOCK_INFO(STOCK_ID, NAME, INDUSTRY, MARKET, SYMBOL, LANG) VALUES (@STOCK_ID, @NAME, @INDUSTRY, @MARKET, @SYMBOL, @LANG) ON DUPLICATE KEY UPDATE MARKET=@MARKET";

        private const string GET_STOCKSINFO = "SELECT * FROM G_STOCK_INFO";
        #endregion

        #region IStocksInfo Members


        public List<StocksInfoData> getAllStocksInfo()
        {
            try
            {
                List<StocksInfoData> list = new List<StocksInfoData>();

                MySqlDataReader rdr = MyHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_STOCKSINFO);

                while (rdr.Read())
                {
                    list.Add(new StocksInfoData(
                                Convert.ToInt32(rdr["STOCK_ID"]),
                                rdr["SYMBOL"].ToString(),
                                rdr["NAME"].ToString(),
                                Convert.ToInt32(rdr["INDUSTRY"]),
                                rdr["LANG"].ToString(),
                                rdr["MARKET"].ToString()));
                }

                rdr.Close();
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public StocksInfoData getStocksInfoById(int stockId)
        {
            throw new NotImplementedException();
        }

        public void deleteStocksInfo()
        {
            throw new NotImplementedException();
        }

        public void insertStocksInfo(StocksInfoData stock)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                    new MySqlParameter( PARM_STOCKID,MySqlDbType.Int32),
                    new MySqlParameter( PARM_SYMBOL,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_NAME,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_INDUSTRY,MySqlDbType.Int16),
                    new MySqlParameter( PARM_LANG,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_MARKET,MySqlDbType.VarChar)
                };

                parms[0].Value = stock.StockId;
                parms[1].Value = stock.Symbol;
                parms[2].Value = stock.Name;
                parms[3].Value = stock.Industry;
                parms[4].Value = stock.Lang;
                parms[5].Value = stock.Market;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_STOCKSINFO, parms);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
